package com.yonder.producer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonder.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @ClassName Producer
 * @Description TODO
 * @Author zxs
 * @Date 2021/5/9 16:40
 * Version 1.0
 */
@Component
public class Producer {
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    public void send() throws Exception {
        Message message = new Message();
        message.setId(System.currentTimeMillis());
        message.setMsg(UUID.randomUUID().toString());
        //设置主题
        kafkaTemplate.send("yonder", objectMapper.writeValueAsString(message));
    }
}
